Summing numbers: Exercise 2.2
Python comes with a built-in sum function. That function takes a sequence
of numbers, and returns the sum of those numbers. So if you were to invoke sum([1,2,3]), the result would be 6.
The challenge here is to write a mysum function that does the same thing as the built-in sum function. However, instead of taking a single sequence as a parameter, it should take a variable number of parameters. Thus while we might invoke sum([1,2,3]) , we would instead invoke mysum(1,2,3).
In [1]:
def mysum(seq):
return reduce(lambda s, n: s + n, seq, 0)
In [2]:
print mysum([1,2,3,4])
In [ ]: